#include "syscall.h"
#include "user.h"

void main()
{
  int whichtest;
  int n;
  int i;
  char name[6];
  
  while(1){
  Printf("\nPlease choose which test to run\n");
  Printf("test1: createLock test\n");
  Printf("test2: destoryLock test\n");
  Printf("test3: acquireLock test\n");
  Printf("test4: releaseLock test\n");
  Printf("test5: createMV test\n");
  Printf("test6: destoryMV test\n");
  Printf("test7: getMVvalue test\n");
  Printf("test8: setMVvalue test\n");
  Printf("test9: createCV test\n");
  Printf("test10: deleteCV test\n");
  Printf("test11: waitCV test\n");
  Printf("test12: signalCV test\n");
  Printf("test13: broadcastCV test\n");
  
  
  whichtest=InputInt();
  switch(whichtest)
  {
	case 1:
	n=rpcCreateLock("wuyuzhe",sizeof("wuyuzhe"));
	n=rpcCreateLock("churutan",sizeof("churutan"));
	n=rpcCreateLock("wuyuzhe",sizeof("wuyuzhe"));
	n=rpcCreateLock("create1",sizeof("create1"));
	n=rpcCreateLock("create2",sizeof("create2"));
	n=rpcCreateLock("create3",sizeof("create3"));
	n=rpcCreateLock("create4",sizeof("create4"));
	n=rpcCreateLock("create5",sizeof("create5"));
/*	n=rpcCreateLock("create6",sizeof("create6"));
*	n=rpcCreateLock("create7",sizeof("create7"));
*	n=rpcCreateLock("create8",sizeof("create8"));
*	n=rpcCreateLock("create9",sizeof("create9"));
*/
	break;
  
	case 2:
	n=rpcCreateLock("yuzhewu",sizeof("yuzhewu"));
	rpcDestroyLock(n);
	Printf("Destory a lock that doesn't exist or already being destroyed\n");
	rpcDestroyLock(n+10);
	Printf("Destory a lock in use \n");
	n=rpcCreateLock("yuzhewu",sizeof("yuzhewu"));
	Printf("I want to acquire lock# %d\n",n);
	rpcAcquireLock(n);
	rpcDestroyLock(n);
	break;
	
	case 3:
	n=rpcCreateLock("shuji",sizeof("shuji"));
	rpcAcquireLock(n);
	Printf("Acquire a lock that is owned by itself\n");
	rpcAcquireLock(n);
	Printf("Acquire a lock of wrong index\n");
	rpcAcquireLock(n-100);
	rpcAcquireLock(n+6);
	
	break;
	
	case 4:
	n=rpcCreateLock("dapangzi",sizeof("dapangzi"));
	Printf("try to release a lock that is not owned by it\n");
	rpcReleaseLock(n);
	Printf("now release a lock normally\n");
	rpcAcquireLock(n);
	rpcReleaseLock(n);
	break;
	
	case 5:
	n=rpcCreateMV("gandie",sizeof("gandie"),1,0);
	Printf("we try to create a MV of the same name\n");
	n=rpcCreateMV("gandie",sizeof("gandie"),1,0);
	n=rpcCreateMV("fanshu",sizeof("fanshu"),3,1);
	n=rpcCreateMV("MV#1",sizeof("MV#1"),2,0);
	n=rpcCreateMV("MV#2",sizeof("MV#1"),2,0);
	n=rpcCreateMV("MV#3",sizeof("MV#1"),2,0);
	n=rpcCreateMV("MV#4",sizeof("MV#1"),2,0);
	n=rpcCreateMV("MV#5",sizeof("MV#1"),2,0);
	n=rpcCreateMV("MV#6",sizeof("MV#1"),2,0);
	n=rpcCreateMV("MV#7",sizeof("MV#1"),2,0);
	n=rpcCreateMV("MV#8",sizeof("MV#1"),2,0);
	n=rpcCreateMV("MV#9",sizeof("MV#1"),2,0);
	
	break;
	
	case 6:
	n=rpcCreateMV("qiukuli",sizeof("qiukuli"),2,0);
	rpcDestroyMV(n);
	Printf("we try to destory a MV that doesn't exists or already destroyed\n");
	rpcDestroyMV(n);
	rpcDestroyMV(n+5);
	
	break;
	
	case 7:
	Printf("now we create a MV consists of 3 element,all of whose value is 0\n");
	n=rpcCreateMV("caomushen",sizeof("caomushen"),3,0);
	Printf("try to get value from a non-exsited MV\n");
	i=rpcGetValueMV(n+5,2);
	Printf("try to get value from an illegal element\n");
	i=rpcGetValueMV(n,4);
	i=rpcGetValueMV(n,2);
	Printf("The value we obtain is %d\n",i);
	
	break;
	
	case 8:
	n=rpcCreateMV("lajitong",sizeof("lajitong"),3,0);
	Printf("the case of set value of wrong index\n");
	rpcSetValueMV(n+5,2,0);
	i=rpcGetValueMV(n,2);
	Printf("the initial value of the index is %d\n",i);
	Printf("now we set the value to 5\n");
	rpcSetValueMV(n,2,5);
	i=rpcGetValueMV(n,2);
	Printf("the new value is %d at this time\n",i);
	
	break;
	
	case 9:
	n=rpcCreateCondition("ruishu",sizeof("ruishu"));
	Printf("try to create a CV of the same name\n");
	
 	n=rpcCreateCondition("ruishu",sizeof("ruishu"));
  
	n=rpcCreateCondition("CV#1",sizeof("CV#1"));
	n=rpcCreateCondition("CV#2",sizeof("CV#1"));
	n=rpcCreateCondition("CV#3",sizeof("CV#1"));
	n=rpcCreateCondition("CV#4",sizeof("CV#1"));
	n=rpcCreateCondition("CV#5",sizeof("CV#1"));
	n=rpcCreateCondition("CV#6",sizeof("CV#1"));
	n=rpcCreateCondition("CV#7",sizeof("CV#1"));
	n=rpcCreateCondition("CV#8",sizeof("CV#1"));
	n=rpcCreateCondition("CV#9",sizeof("CV#1"));
	break;
	
	case 10:
	n=rpcCreateCondition("nigepianzi",sizeof("nigepianzi"));
	rpcDestroyCondition(n);
	rpcDestroyCondition(n+5);
	break;
	
	case 11:
	n=rpcCreateCondition("weiqiaosong",sizeof("weiqiaosong"));
	i=rpcCreateLock("sanye",sizeof("sanye"));
	Printf("gain the ownership of the lock sanye\n");
	rpcAcquireLock(i);
	rpcWaitCV(i,n);
	Printf("Successfully complete the wait process\n");
	rpcReleaseLock(i);
	
	break;
	
	case 12:
	n=rpcCreateCondition("weiqiaosong",sizeof("weiqiaosong"));
	i=rpcCreateLock("sanye",sizeof("sanye"));
	rpcAcquireLock(i);
	Printf("Signal the waited thread\n");
	rpcSignalCV(i,n);
	Printf("Complete the signal process\n");
	rpcReleaseLock(i);
	
	break;
	
	case 13:
	n=rpcCreateCondition("weiqiaosong",sizeof("weiqiaosong"));
	i=rpcCreateLock("sanye",sizeof("sanye"));
	rpcAcquireLock(i);
	Printf("We broadcast all the waited client now\n");
	rpcBroadcastCV(i,n);
	Printf("Complete the broadcast process\n");
	rpcReleaseLock(i);
	break;
	
	default:
	Printf("Enter a wrong number\n");
	break;
}
}
Exit(0);
}